using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace Daniel.Core.Models.Mapping
{
    public class DiscountMap : EntityTypeConfiguration<Discount>
    {
        public DiscountMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(200);

            this.Property(t => t.CouponCode)
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("Discount");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.DiscountTypeId).HasColumnName("DiscountTypeId");
            this.Property(t => t.UsePercentage).HasColumnName("UsePercentage");
            this.Property(t => t.DiscountPercentage).HasColumnName("DiscountPercentage");
            this.Property(t => t.DiscountAmount).HasColumnName("DiscountAmount");
            this.Property(t => t.StartDateUtc).HasColumnName("StartDateUtc");
            this.Property(t => t.EndDateUtc).HasColumnName("EndDateUtc");
            this.Property(t => t.RequiresCouponCode).HasColumnName("RequiresCouponCode");
            this.Property(t => t.CouponCode).HasColumnName("CouponCode");
            this.Property(t => t.DiscountLimitationId).HasColumnName("DiscountLimitationId");
            this.Property(t => t.LimitationTimes).HasColumnName("LimitationTimes");

            // Relationships
            this.HasMany(t => t.Categories)
                .WithMany(t => t.Discounts)
                .Map(m =>
                    {
                        m.ToTable("Discount_AppliedToCategories");
                        m.MapLeftKey("Discount_Id");
                        m.MapRightKey("Category_Id");
                    });

            this.HasMany(t => t.Products)
                .WithMany(t => t.Discounts)
                .Map(m =>
                    {
                        m.ToTable("Discount_AppliedToProducts");
                        m.MapLeftKey("Discount_Id");
                        m.MapRightKey("Product_Id");
                    });


        }
    }
}
